<?php
/**
 * app控制器虚拟类
 */
class AppController extends Controller {

      protected $error = array();

	protected function jsonEncode($status, $data = [], $message = '') {
            $status   = boolval($status);
		$data     = $data ? $data : array();
		$message  = strval($message);
            
            header('content-type:application/json;charset=utf8');
		header('Access-Control-Allow-Origin: *');
		header('Access-Control-Allow-Headers:Origin, Content-Type, X-Auth-Token');
            header('Access-Control-Allow-Method:GET,POST');
            
		if (strtoupper($_SERVER["REQUEST_METHOD"] == "OPTIONS")) {
			$this->jsonError([], "Invalid request");
			exit;
            }

            $result = [
			'status'         => $status,
			'message'        => $message,
			'data'           => $data
		];
            print json_encode($result);
		exit;
      }

      public function jsonSuccess($data = [], $message = '') {
            $message = $message ? $message : 'success';
            $this->jsonEncode(true, $data, $message);
      }

      public function jsonError($data = [], $message = '') {
            $message = $message ? $message : 'failure';
            $this->jsonEncode(false, $data, $message);
      }
      
}